<?php
namespace Admin\Model;
use Think\Model;
class ProductModel extends Model {
	const PARAM_KEY = 'PRODUCT_RISK_RANK'; //产品系统参数表参数 KEY

	/**增加产品操作*/
	public function add_Product( $add_data ){
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		if( !$add_data['name'] || !$add_data['manager_id'] )
			$r['i'] = "表单信息获取失败";
		else{
			//同步企业号通讯录
			/*$map = array();
			$map['product_line.product_line_code'] = array('eq',$add_data['product_line_code']);
			$map['product_line.is_deleted'] = array('neq',1);
			$map['qy.is_deleted'] = array('neq', 1);
			$qyInfo = M("product_line")
						->field("product_line.qy_deparment_id,product_line.name,qy.qy_code")
						->join("qy on product_line.product_line_code=qy.product_line_code")
						->where($map)
						->select();
			if($qyInfo){
				$qyhObj = C("QYH");
				$maxId = D('ProductPackage')->getMaxId();//获取数据表中qy_deparment_id的最大值并自增1
				for ($i=0; $i < count($qyInfo); $i++) {
					foreach ($qyhObj as $qyk => $qyval) {
						//判断企业号是否在配置文件中
						if($qyval['corpCode'] == $qyInfo[$i]['qy_code']){
							//判断该企业号下产品线是否存在
							$MQy[$i] =  new \Common\ORG\Qy($qyInfo[$i]['qy_code']);
							$queryDate[$i] = $MQy[$i]->queryDepartment((int)$qyInfo[$i]['qy_deparment_id']);
							if($queryDate[$i]){
								foreach ($queryDate[$i]['data'] as $Dk => $Dval) {
									$queryDateArr[] = $Dval;
								}
							}
							if($queryDate[$i]['rcode'] && $queryDateArr[0]['name']==$qyInfo[$i]['name']){ //产品线存在，直接添加产品
								$MQy1[$i] =  new \Common\ORG\Qy($qyInfo[$i]['qy_code']);
								$returnDate[$i] = $MQy1[$i]->createDepartment($add_data['name'], (int)$qyInfo[$i]['qy_deparment_id'], $maxId);
								if($returnDate[$i]['rcode'] == 0){
									$r['r'] = 0;
									$r['i'] = '同步企业号通讯录失败-'.$returnDate[$i]['msg'];
									return $r;
								}
							}else if(!$queryDate[$i]['rcode'] || $queryDate[$i]['rcode'] && $queryDate[$i][0]['name']!=$qyInfo[$i]['name'] ){	
							//不存在，先创建产品线
								$Qyh[$i] =  new \Common\ORG\Qy($qyInfo[$i]['qy_code']);
								$reD[$i] = $Qyh[$i]->createDepartment($qyInfo[$i]['name'], 1, $maxId);
								if($reD[$i]['rcode'] == 0){
									$r['r'] = 0;
									$r['i'] = '同步企业号通讯录失败-'.$reD[$i]['msg'];
									return $r;
								}else{
									//添加产品线成功后同步表中部门id
									$maps[$i] = array();
									$maps[$i]['product_line_code'] = array('eq',$add_data['product_line_code']);
									$upd[$i] = array();
									$upd[$i]['qy_deparment_id'] = $maxId;
									$updateLine[$i] = M('product_line')->where($maps[$i])->save($upd[$i]);
									//同步产品信息
									$MQyh[$i] =  new \Common\ORG\Qy($qyInfo[$i]['qy_code']);
									$deparid[$i] = D('ProductPackage')->getMaxId();
									$returnDates[$i] = $MQyh[$i]->createDepartment($add_data['name'], $maxId, $deparid[$i]);
									if($returnDates[$i]['rcode'] == 0){
										$r['r'] = 0;
										$r['i'] = '同步企业号通讯录失败-'.$returnDates[$i]['msg'];
										return $r;
									}
								}
							}
						}
					}
				}

				$d = array();
				$d = $add_data;
				$d['create_time'] = date('Y-m-d H:i:s');
				$d['qy_deparment_id'] = D('ProductPackage')->getMaxId();//获取数据表中qy_deparment_id的最大值并自增1
				$addID = $this->add( $d );
				$r['r']=1;
				$r['i']='ok';
				
			}else{
				$r['i'] = '获取企业号信息失败！';
			}*/

			//crm3数据库中添加产品信息
			$d = array();
			$d = $add_data;
			$d['create_time'] = date('Y-m-d H:i:s');
			$d['qy_deparment_id'] = D('ProductPackage')->getMaxId();//获取数据表中qy_deparment_id的最大值并自增1
			$addID = $this->add( $d );
			$r['r']=1;
			$r['i']='ok';
		}
		return $r;
	}

	/** 更新产品操作*/
	public function update_Product($edit_data){
		$r = array('r'=>0,'d'=>NULL,'m'=>'OK' );
		$dproduct = null;
		$wheres = array();
		$wheres['product_id'] = array('eq', $edit_data['product_id'] );
		$dproduct = $this->where( $wheres )->find();
		if(!$dproduct){
			$r['i'] = "产品信息获取失败";
			return $r;
		}else{
			$d['name'] = $edit_data['name'];
			$d['remark'] = $edit_data['remark'];
			$d['manager_id'] = $edit_data['manager_id'];
			$d['risk_rank'] = $edit_data['risk_rank'];
			$editOrNot = $this->where( $d )->find();
			if(!$editOrNot){	//修改产品信息
				/*if($dproduct['name']!=$edit_data['name']){
					//修改了产品名称,同步企业号通讯录
					$map = array();
					$map['product.product_id'] = array('eq',$edit_data['product_id']);
					$map['product_line.is_deleted'] = array('neq',1);
					$map['qy.is_deleted'] = array('neq', 1);
					$qyInfo = M("product_line")
								->field("product.qy_deparment_id,product_line.name,qy.qy_code")
								->join("qy on product_line.product_line_code=qy.product_line_code")
								->join("product on product.product_line_code=qy.product_line_code")
								->where($map)
								->select();
					if($qyInfo){
						$qyhObj = C("QYH");
						for ($i=0; $i < count($qyInfo); $i++){
							foreach ($qyhObj as $qyk => $qyval){
								//判断企业号是否在配置文件中
								if($qyval['corpCode'] == $qyInfo[$i]['qy_code']){
									$MQy[$i] =  new \Common\ORG\Qy($qyInfo[$i]['qy_code']);
									$returnDate[$i] = $MQy[$i]->updateDepartment($qyInfo[$i]['qy_deparment_id'], $edit_data['name']);
									if($returnDate[$i]['rcode'] == 0){
										$r['r'] = 0;
										$r['i'] = '同步企业号通讯录失败-'.$returnDate[$i]['msg'];
										return $r;
									}
								}
							}
						}
					}else{
						$r['i'] = '获取企业号信息失败！';
					}
				}*/
				$d['name'] = $edit_data['name'];
				$d['remark'] = $edit_data['remark'];
				$d['manager_id'] = $edit_data['manager_id'];
				$d['risk_rank'] = $edit_data['risk_rank'];
				$editID = $this->where( $wheres )->save( $d );
				if($editID){
					$r['r'] = 1;
					$r['d'] = $this->where( $wheres )->find();
					$r['i'] = 'OK';
				}else{
					$r['i'] = '更新产品信息失败！';
				}
			}else{
				$r['r'] = 2;	//未编辑产品信息
			}
		}
		return $r;
	}
	
	/** 删除产品操作*/
	public function delete_Product( $product_id ){
		$r = array('r'=>0,'i'=>'server error','d'=>NULL );
		if( !$product_id ){
			$r['i'] = '表单信息获取失败';
			return $r;
		}
		$wheres1 = array();
		$wheres1['product.product_id'] = array('eq', $product_id);
		$wheres1['b.is_delete'] = array('neq', 1);
		$dlist1 = M('product')
			  ->join('contract  as  b  on  product.product_id = b.product_id')
			  ->where( $wheres1 )
			  ->select();
		$wheres2 = array();
		$wheres2['product.product_id'] = array('eq', $product_id);
		$wheres2['c.is_deleted'] = array('neq', 1);
  		$dlist2 = M('product')
			  ->join('product_package as c on product.product_id = c.product_id')
			  ->where( $wheres2 )
			  ->select();
		if($dlist1){
			$r['r'] = 2;
			$r['i'] = "该产品存在关联的合同，暂时无法删除！";
		}
		if($dlist2){
			$r['r'] = 3;
			$r['i'] = "该产品存在关联的产品套餐，暂时无法删除！";
		}
		if(!$dlist1 && !$dlist2){
			$where = array();
			$where['product_id'] = array('eq', $product_id);
			//同步企业号通讯录
			/*$map = array();
			$map['product.product_id'] = array('eq',$product_id);
			$map['product_line.is_deleted'] = array('neq',1);
			$map['qy.is_deleted'] = array('neq', 1);
			$qyInfo = M("product_line")
						->field("product.qy_deparment_id,product_line.name,qy.qy_code")
						->join("qy on product_line.product_line_code=qy.product_line_code")
						->join("product on product.product_line_code=qy.product_line_code")
						->where($map)
						->select();
			if($qyInfo){
				$qyhObj = C("QYH");
				for ($i=0; $i < count($qyInfo); $i++){
					foreach ($qyhObj as $qyk => $qyval){
						//判断企业号是否在配置文件中
						if($qyval['corpCode'] == $qyInfo[$i]['qy_code']){
							$MQy[$i] =  new \Common\ORG\Qy($qyInfo[$i]['qy_code']);
							$returnDate[$i] = $MQy[$i]->deleteDepartment($qyInfo[$i]['qy_deparment_id']);
							if($returnDate[$i]['rcode'] == 0){
								$r['r'] = 0;
								$r['i'] = '同步企业号通讯录失败-'.$returnDate[$i]['msg'];
								return $r;
							}
						}
					}
				}
				$delID = $this->where( $where )->delete();
				if($delID){
					$r['r'] = 1;
					$r['i'] = 'OK';
				}else{
					$r['i'] = '删除失败！';
					$r['r'] = 0;
				}
			}else{
				$r['i'] = '获取企业号信息失败！';
			}*/

			$delID = $this->where( $where )->delete();
			if($delID){
				$r['r'] = 1;
				$r['i'] = 'OK';
			}else{
				$r['i'] = '删除失败！';
				$r['r'] = 0;
			}
		}
 		return $r;
	}

}